package com.thinkingstar.iads.projectcenter.serviceImpl;

import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.common.utils.CommonDateUtil;
import com.thinkingstar.iads.common.utils.StringUtil;
import com.thinkingstar.iads.cs.sys.entity.SysUser;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignCell;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstantVersion;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignFormulaVersion;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstant;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignFormulaData;
import com.thinkingstar.iads.datacenter.entity.DcProjectFieldWidget;
import com.thinkingstar.iads.datacenter.entity.DcProjectMentionInformation;
import com.thinkingstar.iads.datacenter.entity.common.dao.CommonDao;
import com.thinkingstar.iads.datacenter.entity.dao.*;
import com.thinkingstar.iads.projectcenter.entity.PcProjectConstantData;
import com.thinkingstar.iads.projectcenter.entity.PcProjectFormulaData;
import com.thinkingstar.iads.projectcenter.entity.PcProjectInfo;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectData;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.QueryResult;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectConstantData;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectFormulaData;


/**
 * @ClassName: ProjectDcsjServiceImpl
 * @Description: 动车设计维护
 * @author z
 * @date  
 */
@IocBean
public class ProjectDcsjServiceImpl {

	@Inject
	private DaoPcProjectData daoPcProjectData;

	@Inject
	private DaoDcProjectDesignCell daoDcProjectDesignCell;

	@Inject CommonDao commonDao;

	@Inject
	private DaoDcFormField daoDcFormField;

	@Inject
	private DaoPcProjectConstantData daoPcProjectConstantData;

	@Inject
	private DaoPcProjectFormulaData daoPcProjectFormulaData;
	
	@Inject
	private DaoDcProjectFieldWidget daoDcProjectFieldWidget;

	/**
	 * 根据项目id获取数据中心所有常量集（带分页)
	 * */
	public QueryResult getConstantVersionListPage(NutMap param) {
		PcProjectInfo project_info = daoPcProjectData.fetch((String)param.get("project_id"));
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		param.put("phase_id",phase_id);
		param.put("content_id",content_id);
		return daoPcProjectConstantData.getDcProjectConstantVersionPage(param);
	}

	/**
	 * 根据项目id获取数据中心所有常量集(不带分页)(old)
	 * */
	public List<DcProjectDesignConstantVersion> getConstantVersionList(String project_id) {
		PcProjectInfo project_info = daoPcProjectData.fetch(project_id);
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		return daoPcProjectConstantData.getDcProjectConstantVersion(content_id,phase_id);
	}

	/**
	 * 根据项目id和设计单元id获取数据中心所有相关常量集(不带分页)(new)
	 * */
	public List<DcProjectDesignConstantVersion> getConstantVersionList(String project_id,String design_cell_id) {
		PcProjectInfo project_info = daoPcProjectData.fetch(project_id);
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		return daoPcProjectConstantData.getDcProjectConstantVersion(content_id,phase_id,design_cell_id);
	}

	/**
	 * 根据数据中心常量集id获取所有常量和代码
	 * */
	public List<DcProjectDesignConstant> getDcConstantListByVersionId(String version_id) {
		return daoPcProjectConstantData.getDcProjectConstantDataList(version_id);
	}

	/**
	 * 根据项目id获取数据中心所有常量
	 * */
	public List<DcProjectDesignConstant> getDcProjectConstantList(String project_id) {
		List<DcProjectDesignConstantVersion> versionList = getConstantVersionList(project_id);
		List<DcProjectDesignConstant> list = new ArrayList<DcProjectDesignConstant>();
		for(int i=0;i<versionList.size();i++) {
			list.addAll(daoPcProjectConstantData.getDcProjectConstantDataList(versionList.get(i).getId()));
		}
		return list;
	}

	/**
	 * 根据项目id和设计单元id获取数据中心所有相关常量(new)
	 * */
	public List<DcProjectDesignConstant> getDcProjectConstantListByDesignCell(String project_id,String design_cell_id) {
		List<DcProjectDesignConstantVersion> versionList = getConstantVersionList(project_id,design_cell_id);
		List<DcProjectDesignConstant> list = new ArrayList<DcProjectDesignConstant>();
		for(int i=0;i<versionList.size();i++) {
			list.addAll(daoPcProjectConstantData.getDcProjectConstantDataList(versionList.get(i).getId()));
		}
		return list;
	}

	/**
	 * 根据项目id获取数据中心某常量集下的所有常量
	 * */
	public List<DcProjectDesignConstant> getDcProjectConstantList(String project_id,String constant_table_code) {
		List<DcProjectDesignConstantVersion> versionList = getConstantVersionList(project_id);
		List<DcProjectDesignConstant> list = new ArrayList<DcProjectDesignConstant>();
		for(int i=0;i<versionList.size();i++) {
			if (versionList.get(i).getConstant_table_code().equals(constant_table_code)) {
				list.addAll(daoPcProjectConstantData.getDcProjectConstantDataList(versionList.get(i).getId()));
			}
		}
		return list;
	}

	/**
	 * 根据项目id获取数据中心所有公式集(old)
	 * */
	public List<DcProjectDesignFormulaVersion> getFormulaVersionList(String project_id) {
		PcProjectInfo project_info = daoPcProjectData.fetch(project_id);
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		List<DcProjectDesignFormulaVersion> list = daoPcProjectFormulaData.getDcProjectFormulaVersion(content_id,phase_id);
		return list;

	}

	/**
	 * 根据项目id及设计单元id获取数据中心所有相关公式集(new)
	 * */
	public List<DcProjectDesignFormulaVersion> getFormulaVersionList(String design_cell_id,String project_id) {
		PcProjectInfo project_info = daoPcProjectData.fetch(project_id);
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		List<DcProjectDesignFormulaVersion> list = daoPcProjectFormulaData.getDcProjectFormulaVersion(design_cell_id,content_id,phase_id);
		return list;

	}


	/**
	 * 根据项目id获取数据中心所有公式集（带分页)
	 * */
	public QueryResult getFormulaVersionListPage(NutMap param) {
		PcProjectInfo project_info = daoPcProjectData.fetch((String)param.get("project_id"));
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		param.put("phase_id",phase_id);
		param.put("content_id",content_id);
		return daoPcProjectFormulaData.getDcProjectFormulaVersionPage(param);
	}

	/**
	 * 根据数据中心公式集id获取所有公式
	 * */
	public List<DcProjectDesignFormulaData> getDcFormulaListByVersionId(String version_id) {
		return daoPcProjectFormulaData.getDcProjectFormulaDataList(version_id);
	}

	/**
	 * 根据项目id获取数据中心所有公式
	 * */
	public List<DcProjectDesignFormulaData> getDcProjectFormulaDataListByDesignCell(String design_cell_id,String project_id) {
		List<DcProjectDesignFormulaVersion> versionList = getFormulaVersionList(design_cell_id,project_id);
		List<DcProjectDesignFormulaData> list = new ArrayList<DcProjectDesignFormulaData>();
		for(int i=0;i<versionList.size();i++) {
			list.addAll(daoPcProjectFormulaData.getDcProjectFormulaDataList(versionList.get(i).getId()));
		}
		return list;
	}

	/**
	 * 根据项目id获取数据中心某公式集下的所有公式
	 * */
	public List<DcProjectDesignFormulaData> getDcProjectFormulaDataList(String project_id,String formula_table_code) {
		List<DcProjectDesignFormulaVersion> versionList = getFormulaVersionList(project_id);
		List<DcProjectDesignFormulaData> list = new ArrayList<DcProjectDesignFormulaData>();
		for(int i=0;i<versionList.size();i++) {
			if (versionList.get(i).getFormula_table_code().equals(formula_table_code)) {
				list.addAll(daoPcProjectFormulaData.getDcProjectFormulaDataList(versionList.get(i).getId()));
			}
		}
		return list;
	}

	/**
	 * 根据项目id获取项目中心所有公式(old)
	 * */
	public List<PcProjectFormulaData> getPcProjectFormulaDataList(String project_id) {
		return daoPcProjectFormulaData.getPcProjectFormulaDataList(project_id);
	}

	/**
	 * 根据项目id和设计单元获取项目中心所有相关公式(new)
	 * */
	public List<PcProjectFormulaData> getPcProjectFormulaData(String design_cell_id,String project_id) {
		return daoPcProjectFormulaData.getPcProjectFormulaData(design_cell_id,project_id);
	}


	/**
	 * 根据项目id和公式集code 查询项目中心是否已取用
	 * @param project_id 项目id
	 * @param formula_table_code 公式集code
	 * */
	public boolean getPcProjectFormulaVersionIsHasQy(String project_id,String formula_table_code){
		return daoPcProjectFormulaData.getPcProjectConstantVersionIsHasQy(project_id,formula_table_code);
	}

	/**
	 * 根据项目id和常量集code 查询项目中心是否已取用
	 * @param project_id 项目id
	 * @param version_id 常量集id
	 * */
	public boolean getPcProjectConstantVersionIsHasQy(String project_id,String version_id){
		return daoPcProjectConstantData.getPcProjectConstantVersionIsHasQy(project_id,version_id);
	}

	/**
	 * 根据项目id和公式集code 获取项目中心的所有公式
	 * @param project_id 项目id
	 * @param formula_table_code 公式集code
	 * */
	public List<PcProjectFormulaData> getPcProjectFormulaDataList(String project_id,String formula_table_code) {
		return daoPcProjectFormulaData.getPcProjectFormulaDataList(formula_table_code,project_id);
	}

	/**
	 * 根据项目id获取项目中心的所有常量和代码(old)
	 * @param project_id 项目id
	 * */
	public List<PcProjectConstantData> getPcProjectConstantDataList(String project_id) {
		return daoPcProjectConstantData.getPcProjectConstantDataList(project_id);
	}

	/**
	 * 根据项目id和设计单元id获取项目中心的所有常量和代码(new)
	 * @param project_id 项目id
	 * */
	public List<PcProjectConstantData> getPcProjectConstantData(String project_id,String design_cell_id) {
		return daoPcProjectConstantData.getPcProjectConstantData(project_id,design_cell_id);
	}

	/**
	 * 根据项目id和常量集code 获取项目中心的所有常量和代码 (不带分页)
	 * @param project_id 项目id
	 * @param constant_table_code 常量集code
	 * */
	public List<PcProjectConstantData> getPcProjectConstantDataList(String project_id,String constant_table_code) {
		return daoPcProjectConstantData.getPcProjectConstantDataList(constant_table_code,project_id);
	}

	/**
	 * 根据项目id和常量集code 获取项目中心的所有常量和代码 (带分页)
	 * @param param 项目id
	 * @param param 常量集code
	 * */
	public QueryResult getPcProjectConstantDataListPage(NutMap param) {
		return daoPcProjectConstantData.getPcProjectConstantDataListPage(param);
	}

	/**
	 * 根据项目id和公式集code 获取项目中心的所有公式 (带分页)
	 * @param param 项目id
	 * @param param 公式集code
	 * */
	public QueryResult getPcProjectFormulaDataListPage(NutMap param) {
		return daoPcProjectFormulaData.getPcProjectFormulaDataListPage(param);
	}

	/**
	 * 删除项目中心某个常量集下的所有常量（重新取用）
	 * */
	public AjaxResult deletePcProjectConstantByOne(String project_id,String constant_table_code) {
		return daoPcProjectConstantData.deletePcProjectConstantByOne(project_id,constant_table_code);
	}

	/**
	 * 删除项目中心某个公式集下的所有公式（重新取用）
	 * */
	public AjaxResult deletePcProjectFormulaByOne(String project_id,String formula_table_code) {
		return daoPcProjectFormulaData.deletePcProjectFormulaByOne(project_id,formula_table_code);
	}

	/**
	 * 删除项目中心已取用的所有常量（重新一键取用）
	 * */
	public AjaxResult deletePcProjectConstantBatch(String project_id,String design_cell_id) {
		return daoPcProjectConstantData.deletePcProjectConstantBatch(project_id,design_cell_id);
	}

	/**
	 * 删除项目中心已取用所有公式（重新一键取用）
	 * */
	public AjaxResult deletePcProjectFormulaBatch(String project_id,String design_cell_id) {
		return daoPcProjectFormulaData.deletePcProjectFormulaBatch(project_id,design_cell_id);
	}

	/**
	 * 将数据中心公式list插入项目中心
	 * */
	public AjaxResult insertFormulaBatch(List<DcProjectDesignFormulaData> list,String project_id,String design_cell_id){
		return daoPcProjectFormulaData.insertFormulaBatch(list,project_id,design_cell_id);
	}


	/**
	 * 将数据中心常量list插入项目中心
	 * */
	public AjaxResult insertConstantBatch(List<DcProjectDesignConstant> list,String project_id,String design_cell_id){
		return daoPcProjectConstantData.insertConstantBatch(list,project_id,design_cell_id);
	}

	/**
	 * 根据常量id获取常量详情
	 * */
	public PcProjectConstantData getPcProjectConstantDataById(String constant_id) {
		return daoPcProjectConstantData.getConstantDataById(constant_id);
	}

	/**
	 * 根据公式id获取公式详情
	 * */
	public PcProjectFormulaData getPcProjectFormulaDataById(String formula_id) {
		return daoPcProjectFormulaData.getFormulaDataById(formula_id);
	}


	/**
	 * 更新常量
	 * */
	public AjaxResult updatePcProjectConstantData(PcProjectConstantData constantData) {
		 daoPcProjectConstantData.updateObj(constantData);
		 return AjaxResult.success();
	}

	/**
	 * 更新公式
	 * */
	public AjaxResult updatePcProjectFormulaData(PcProjectFormulaData formulaData) {
		daoPcProjectFormulaData.updateObj(formulaData);
		return AjaxResult.success();
	}


	/**
	 * 根据項目id查询设计单元资料
	 * @param  project_id 项目id
	 * @return
	 */
	public List <DcProjectDesignCell> getDesignCellDataListByPid(String project_id){
		PcProjectInfo project_info = daoPcProjectData.fetch(project_id);
		String phase_id = project_info.getProject_design_phase_id();
		String content_id = project_info.getProject_design_content_id();
		return daoDcProjectDesignCell.getDesignCellDataByContent(content_id,phase_id);
	}

	/** 根据表名 projectid 字段名，初始化 查询 select下拉框内容*/
	public List initSelect(String designcell_tablename,String field_name,String project_id) {
		return daoDcProjectDesignCell.initSelect(designcell_tablename,field_name,project_id);
	}

	/**
	 * 根据设计单元编号 获取设计单元表结构及数据
	 * */
	public NutMap getDesignCellTable(NutMap param) {
		String mention_data_id = param.getString("design_cell_id");
		String project_id = param.getString("project_id");
		DcProjectDesignCell dcProjectDesignCell = daoDcProjectDesignCell.fetch(mention_data_id);
		String table_name = new StringBuffer("dc_designcell_").append(dcProjectDesignCell.getDesign_cell_table_code()).toString();
		param.put("table_name", table_name);

		//结构化数据表单表头
		
		List <DcProjectFieldWidget> widget_list = daoDcProjectFieldWidget.findWidgetByTableName(table_name);
		List <NutMap> field_list = daoDcFormField.queryColumnListForDesignCell(table_name);

		QueryResult data_list = commonDao.findDesignCellDataByTableNameAndParam(param,widget_list);

		//统计求和 字段类型为 int double float类型的字段值

		List<Map> countList = daoDcFormField.countFieldByParam(field_list,param);
		NutMap data_map = new NutMap();
		data_map.put("title_list", widget_list);
		data_map.put("data_list", data_list);
		data_map.put("count_list",countList);
		return data_map;
	}

	public DcProjectDesignCell getDcProjectDesignCell(String design_cell_id) {
		DcProjectDesignCell designCell = daoDcProjectDesignCell.fetch(design_cell_id);
		return designCell;
	}
}
